﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using DAL;
using DTO;

namespace BLL.Store
{
    public class Order:BaseStore
    {
        private int _statusID = 0;
        public int StatusID
        {
            get { return _statusID; }
            set { _statusID = value; }
        }

        private int _productID = 0;
        public int ProductID
        {
            get { return _productID; }
            set { _productID = value; }
        }

        public string ProductTitle
        {
            get { return Product.GetProductByID(ProductID).Title; }
        }

        public string Price
        {
            get { return Product.GetProductByID(ProductID).UnitPrice.ToString("#0,000") + " VND"; }
        }

        //public string SmallImageUrl
        //{
        //    get { return Product.GetProductByID(ProductID).SmallImageUrl; }
        //}


        private string _statusTitle = "";
        public string StatusTitle
        {
            get { return _statusTitle; }
            private set { _statusTitle = value; }
        }

        private string _shippingMethod = "";
        public string ShippingMethod
        {
            get { return _shippingMethod; }
            private set { _shippingMethod = value; }
        }

        private decimal _subTotal = 0.0m;
        public decimal SubTotal
        {
            get { return _subTotal; }
            private set { _subTotal = value; }
        }

        public string StringSubTotal
        {
            get { return SubTotal.ToString("#0,000") + " VND"; }
        }

        private decimal _shipping = 0.0m;
        public decimal Shipping
        {
            get { return _shipping; }
            private set { _shipping = value; }
        }

        private string _shippingFirstName = "";
        public string ShippingFirstName
        {
            get { return _shippingFirstName; }
            private set { _shippingFirstName = value; }
        }

        private string _shippingLastName = "";
        public string ShippingLastName
        {
            get { return _shippingLastName; }
            private set { _shippingLastName = value; }
        }

        private string _shippingNumber = "";
        public string ShippingNumber
        {
            get { return _shippingNumber; }
            set { _shippingNumber = value; }
        }

        private string _shippingStreet = "";
        public string ShippingStreet
        {
            get { return _shippingStreet; }
            set { _shippingStreet = value; }
        }

        private string _shippingWard = "";
        public string ShippingWard
        {
            get { return _shippingWard; }
            set { _shippingWard = value; }
        }

        private string _shippingDistrict = "";
        public string ShippingDistrict
        {
            get { return _shippingDistrict; }
            set { _shippingDistrict = value; }
        }

        private string _shippingCity = "";
        public string ShippingCity
        {
            get { return _shippingCity; }
            set { _shippingCity = value; }
        }

        private string _customerEmail = "";
        public string CustomerEmail
        {
            get { return _customerEmail; }
            private set { _customerEmail = value; }
        }

        private string _customerPhone = "";
        public string CustomerPhone
        {
            get { return _customerPhone; }
            private set { _customerPhone = value; }
        }

        private string _customerFax = "";
        public string CustomerFax
        {
            get { return _customerFax; }
            private set { _customerFax = value; }
        }

        private string _transactionID = "";
        public string TransactionID
        {
            get { return _transactionID; }
            set { _transactionID = value; }
        }

        private DateTime _shippedDate = DateTime.MinValue;
        public DateTime ShippedDate
        {
            get { return _shippedDate; }
            set { _shippedDate = value; }
        }

        private string _trackingID = "";
        public string TrackingID
        {
            get { return _trackingID; }
            set { _trackingID = value; }
        }
        

        public Order(int id, DateTime addedDate, string addedBy, int productID,
           int statusID, string shippingMethod, decimal subTotal, decimal shipping,
           string shippingFirstName, string shippingLastName, string shippingNumber, string shippingStreet,
           string shippingWard, string shippingDistrict, string shippingCity,
           string customerEmail, string customerPhone, string customerFax, string transactionID)
        {
            this.ID = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.ProductID = productID;
            this.StatusID = statusID;
            this.ShippingMethod = shippingMethod;
            this.SubTotal = subTotal;
            this.Shipping = shipping;
            this.ShippingFirstName = shippingFirstName;
            this.ShippingLastName = shippingLastName;
            this.ShippingNumber = shippingNumber;
            this.ShippingStreet = shippingStreet;
            this.ShippingWard = shippingWard;
            this.ShippingDistrict = shippingDistrict;
            this.ShippingCity = shippingCity;
            this.CustomerEmail = customerEmail;
            this.CustomerPhone = customerPhone;
            this.CustomerFax = customerFax;
            this.TransactionID = transactionID;
        }

        /***********************************
        * methods
        ************************************/

        public static List<Order> ListOrders()
        {
            return ListOrders(BizObject.MAXROWS,0);
        }

        public static List<Order> GetOrdersByStatus(int statusID)
        {
            if (statusID==0)
                return ListOrders(BizObject.MAXROWS,0);
            else
                return GetOrdersByStatus(statusID, BizObject.MAXROWS, 0);
        }

        public static List<Order> GetOrdersByProductID(int productID)
        {
            return GetOrdersByProductID(productID, BizObject.MAXROWS, 0);
        }

        /***********************************
        * Static methods
        ************************************/

        public static List<Order> ListOrders(int pageSize, int pageIndex)
        {
            List<Order> orders = null;
            string key = "Store_Order_" + pageSize.ToString() + "_" + pageIndex.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                orders = (List<Order>)BizObject.Cache[key];
            }
            else
            {
                List<OrderDetails> recordset = SiteProvider.Store.ListOrders(pageSize,pageIndex);
                orders = GetOrderListFromOrderDetailsList(recordset);
                BaseStore.CacheData(key, orders);
            }
            return orders;
        }

        /// <summary>
        /// Returns an Order object with the specified ID
        /// </summary>
        public static Order GetOrderByID(int orderID)
        {
            Order order = null;
            string key = "Store_Order_" + orderID.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                order = (Order)BizObject.Cache[key];
            }
            else
            {
                order = GetOrderFromOrderDetails(SiteProvider.Store.GetOrderByID(orderID));
                BaseStore.CacheData(key, order);
            }
            return order;
        }

        public static List<Order> GetOrdersByStatus(int statusID, int pageSize, int pageIndex)
        {
            List<Order> orders = null;
            string key = "Store_Order_" + statusID + "_" + pageSize.ToString() + "_" + pageIndex.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                orders = (List<Order>)BizObject.Cache[key];
            }
            else
            {
                List<OrderDetails> recordset = SiteProvider.Store.GetOrdersByStatus(statusID, pageSize, pageIndex);
                orders = GetOrderListFromOrderDetailsList(recordset);
                BaseStore.CacheData(key, orders);
            }
            return orders;
        }

        public static List<Order> GetOrdersByProductID(int productID, int pageSize, int pageIndex)
        {
            List<Order> orders = null;
            string key = "Store_Order_" + productID + "_" + pageSize.ToString() + "_" + pageIndex.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                orders = (List<Order>)BizObject.Cache[key];
            }
            else
            {
                List<OrderDetails> recordset = SiteProvider.Store.GetOrdersByProductID(productID, pageSize, pageIndex);
                orders = GetOrderListFromOrderDetailsList(recordset);
                BaseStore.CacheData(key, orders);
            }
            return orders;
        }
        

        /// <summary>
        /// Updates an existing order
        /// </summary>
        public static bool UpdateOrder(int id, int statusID)
        {           
            OrderDetails record = new OrderDetails(id, DateTime.Now, "", 0, statusID,"", 0.0m, 0.0m,
               "", "", "", "", "", "", "", "", "", "","");
            bool ret = SiteProvider.Store.UpdateOrder(record);
            BizObject.PurgeCacheItems("store_order");
            return ret;
        }

        /// <summary>
        /// Deletes an existing order
        /// </summary>
        public static bool DeleteOrder(int id)
        {
            bool ret = SiteProvider.Store.DeleteOrder(id);
            BizObject.PurgeCacheItems("store_order");
            return ret;
        }

        /// <summary>
        /// Creates a new order
        /// </summary>
        public static int InsertOrder(int productID, int statusID, string shipNumber, decimal subTotal, string name, string phone,string email, string description)
        {
            string userName = BizObject.CurrentUserName;
            OrderDetails order = new OrderDetails(0, DateTime.Now, "", productID, statusID, "", subTotal, 0.0m,
               name, description, shipNumber, "", "", "", "", email, phone, "", "");
            int orderID = SiteProvider.Store.InsertOrder(order);
            BizObject.PurgeCacheItems("store_order");
            return orderID;
        }

        /// <summary>
        /// Returns a Order object filled with the data taken from the input OrderDetails
        /// </summary>
        private static Order GetOrderFromOrderDetails(OrderDetails record)
        {
            if (record == null)
                return null;
            else
            {
                return new Order(record.ID, record.AddedDate, record.AddedBy, record.StatusID, record.ProductID,
                   record.ShippingMethod, record.SubTotal, record.Shipping, record.ShippingFirstName,
                   record.ShippingLastName, record.ShippingNumber, record.ShippingStreet, record.ShippingWard, record.ShippingDistrict, record.ShippingCity,
                   record.CustomerEmail, record.CustomerPhone,
                   record.CustomerFax, record.TransactionID);
            }
        }

        /// <summary>
        /// Returns a list of Order objects filled with the data taken from the input list of OrderDetails
        /// </summary>
        private static List<Order> GetOrderListFromOrderDetailsList(List<OrderDetails> recordset)
        {
            List<Order> orders = new List<Order>();
            foreach (OrderDetails record in recordset)
                orders.Add(GetOrderFromOrderDetails(record));
            return orders;
        }
    }
}

